{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-10-30T07:55:42.033313Z",
     "start_time": "2024-10-30T07:55:42.029945Z"
    }
   },
   "source": [
    "class Animal:\n",
    "    def speak(self):\n",
    "        pass\n",
    " \n",
    "class Dog(Animal):\n",
    "    def speak(self):\n",
    "        print(\"Woof!\")\n",
    " \n",
    "class Cat(Animal):\n",
    "    def speak(self):\n",
    "        print(\"Meow!\")\n",
    " \n",
    "def make_sound(animal):\n",
    "    if isinstance(animal, Dog):\n",
    "        animal.speak()\n",
    "    elif isinstance(animal, Cat):\n",
    "        animal.speak()\n",
    "    else:\n",
    "        print(\"未知动物\")\n",
    " \n",
    "dog = Dog()\n",
    "cat = Cat()\n",
    "make_sound(dog)  # 输出：Woof!\n",
    "make_sound(cat)  # 输出：Meow!\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Woof!\n",
      "Meow!\n"
     ]
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
